package dk.degofedal.scala.math

class Phasor(amplitude : Double, phase : Double, frequency : Double) {
	
	val freq = frequency
	val amp = amplitude
	val ph = phase
	
	def *(B : Double):Phasor = {
		* (B , 0)
	}
	
	def *(B : Double, p : Double):Phasor = {
		new Phasor( amplitude * B , phase + p , frequency )
	}
	
	def +(P : Phasor):Phasor = {
		if ( P.freq != frequency) {
			throw new RuntimeException("Frequencies must match "+P+" : "+this);
		}
		val cos_part = amplitude * Math.cos(phase) + P.amp * Math.cos(P.ph);
		val sin_part = amplitude * Math.sin(phase) + P.amp * Math.sin(P.ph);
		
		new Phasor(Math.sqrt(Math.pow(cos_part, 2) + Math.pow(sin_part, 2)), Math.atan(sin_part / cos_part), frequency)
	}
	
	override def toString () = {
		"P(" + amplitude + ", "+frequency + ", " + phase + ")" 
	}
	
}